package dolf.objects.weapons.Jetpack;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import dolf.game.Level;
import dolf.game.Player;
import dolf.objects.CircleObject;
import dolf.objects.ImageStore;
import dolf.objects.Point;
import dolf.objects.Vector;
import dolf.objects.weapons.*;
import dolf.objects.*;
import dolf.window.SystemMessagesHandler;


public class Jetpack extends CircleObject implements Weapon, java.io.Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int Price = 125;
	public static final String Name = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Jetpack.lang.lang").getString("name");
	public static final String WeaponType = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Jetpack.lang.lang").getString("type");
	public static final String Description = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Jetpack.lang.lang").getString("description");
	public static final javax.swing.ImageIcon Icon = ImageStore.getInstance().getIcon("dolf/objects/weapons/Jetpack/gfx/icon.png");
	

	private final int MAX_FUEL = 20;
	private AffineTransform affineTransform;
	private BufferedImage image;
	private int fuel;
	private int flame;
	private Player pl;
	private boolean inititalized = false;
	private double oldAngle;
	transient private Image img;
	transient private Image imgf;
	
	public Jetpack(){
		super(null,11,false);
		this.setCollitabe(false);
		
		img = ImageStore.getInstance().getImage("dolf/objects/weapons/Jetpack/gfx/icon_v.png");
		img = img.getScaledInstance(2*this.getRadius(), 2*this.getRadius(), Image.SCALE_SMOOTH);
		
		imgf = ImageStore.getInstance().getImage("dolf/objects/weapons/Jetpack/gfx/icon_vf.png");
		imgf = imgf.getScaledInstance(2*this.getRadius(), 2*this.getRadius(), Image.SCALE_SMOOTH);
		flame = 0;
		fuel = MAX_FUEL;
	}
	public void initialize(Player _pl){
		if(this.inititalized){
			return;
		}
		this.oldAngle = 0;
		this.inititalized = true;
		this.pl = _pl;
		this.pl.setWeaponState();
		Level.getInstance().registerObjectOnTop(this);
	}
	public void deInitialize(){
		Level.getInstance().removeObject(this);
		this.inititalized = false;
		this.pl = null;
	}
	
	public void paint(Graphics2D g){
		if (img == null) {
	       	img = ImageStore.getInstance().getImage("dolf/objects/weapons/Jetpack/gfx/icon_v.png");
	    	img = img.getScaledInstance(2*this.getRadius(), 2*this.getRadius(), Image.SCALE_SMOOTH);
	    }
	    if (imgf == null) {
	    	imgf = ImageStore.getInstance().getImage("dolf/objects/weapons/Jetpack/gfx/icon_vf.png");
	    	imgf = imgf.getScaledInstance(2*this.getRadius(), 2*this.getRadius(), Image.SCALE_SMOOTH);
	    }
	    
		double angle = Math.atan2(Level.getInstance().getYMouse()-this.getPosition().getY(), Level.getInstance().getXMouse()-this.getPosition().getX())-(Math.PI/2);
		Graphics2D g2;
		if(angle != this.oldAngle){
			int mask = BufferedImage.TYPE_3BYTE_BGR| BufferedImage.TYPE_INT_ARGB; 
			affineTransform = AffineTransform.getRotateInstance(angle,this.getRadius(), this.getRadius());
			image = new BufferedImage (2*this.getRadius(), 2*this.getRadius(),mask);
			g2 = (Graphics2D) image.getGraphics();
			if(flame>0){
	        	g2.drawImage(imgf, affineTransform, null);
	        	flame--;
	        }else{
	        	g2.drawImage(img, affineTransform, null);
	        }
			this.oldAngle = angle;
		} else {
			if(image == null){
				int mask = BufferedImage.TYPE_3BYTE_BGR| BufferedImage.TYPE_INT_ARGB; 
				image = new BufferedImage (2*this.getRadius(), 2*this.getRadius(),mask);
			}
			if(flame>0){
				flame--;
			}
		}
       		    
		g.drawImage(image, this.getPosition().getIntX()-this.getRadius(), this.getPosition().getIntY()-this.getRadius(), null);
	}
	
	public void execute() {
		if(Level.getInstance().mousePressed()&& pl.getBall().isMoving()){
			if(this.fuel > 0){
				Vector tmp = new Vector(Level.getInstance().getXMouse(),Level.getInstance().getYMouse());
				tmp = tmp.sub(this.getPosition()).setLength(0.1);
				pl.getBall().setMotion(pl.getBall().getMotion().sub(tmp));
				this.fuel--;
				this.pl.setWeaponState();
				flame = 15;
				return;
			} else {
				System.err.println("out of Fuel!");
			}
		}	

	}
	public boolean newMotion(WorldObject object){
		return true;
	}	
	public ImageIcon getIcon() {
		return Icon;//ImageStore.getInstance().getIcon("dolf/objects/weapons/Jetpack/gfx/icon.png");	
	}

	public String getName() {
		return Name;
		/*if (messages == null) loadProperties();
		return messages.getString("name");*/
	}
	
	public String getDescription() {
		return Description;
		/*if (messages == null) loadProperties();
		return messages.getString("description");*/
	}
	public String getType(){
		return WeaponType;
	}

	public int getPrice() {
		return Price;
	}

	public void remove(){
		Level.getInstance().removeObject(this);
		pl.removeWeapon(this);
		this.pl.setWeaponState();
		this.pl.setAktWeapon(null);
	}
	public Point getPosition(){
		return pl.getBall().getPosition();
	}
	public Player getPlayer(){
		return this.pl;
	}
	public double getState(){
		return (double)this.fuel/this.MAX_FUEL;
	}
}
